package com.abc.cloud.service.impl;
import java.util.Date;

import cn.hutool.core.bean.BeanUtil;
import com.abc.cloud.common.exception.MyException;
import com.abc.cloud.dto.im.ContactsSearch;
import com.abc.cloud.dto.im.ContactsUserDto;
import com.abc.cloud.dto.user.UserDto;
import com.abc.cloud.entity.ChatEntity;
import com.abc.cloud.entity.GroupEntity;
import com.abc.cloud.entity.UserEntity;
import com.abc.cloud.service.ChatService;
import com.abc.cloud.service.GroupService;
import com.abc.cloud.service.UserService;
import com.abc.cloud.vo.im.AdminUpdateContactsReq;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.abc.cloud.common.utils.PageUtil;
import com.abc.cloud.common.utils.Query;

import com.abc.cloud.dao.ContactsDao;
import com.abc.cloud.entity.ContactsEntity;
import com.abc.cloud.service.ContactsService;


@Service("contactsService")
public class ContactsServiceImpl extends ServiceImpl<ContactsDao, ContactsEntity> implements ContactsService {

    @Autowired
    private UserService userService;

    @Autowired
    private ChatService chatService;

    @Override
    public PageUtil queryPage(Map<String, Object> params) {
        IPage<ContactsEntity> page = this.page(
                new Query<ContactsEntity>().getPage(params),
                new QueryWrapper<ContactsEntity>()
        );

        return new PageUtil(page);
    }

    @Override
    public ContactsSearch search(String searchContent) {
        ContactsSearch res = new ContactsSearch();
        // 搜索用户
        List<UserEntity> userList = userService.list(new QueryWrapper<UserEntity>().eq("username", searchContent));
        List<UserDto> userDtoList = BeanUtil.copyToList(userList, UserDto.class);
        res.setUserList(userDtoList);
        // 搜索群聊
        List<ChatEntity> chatEntityList = chatService.list(new QueryWrapper<ChatEntity>().eq("id", searchContent)
                                                            .eq("type", 2));
        res.setChatList(chatEntityList);

        return res;
    }

    @Override
    public List<ContactsUserDto> queryByGroupId(Long groupId) {
        return this.baseMapper.queryByGroupId(groupId);
    }

    @Override
    public void saveContacts(Long userId, Long toUserId, String note, Long groupId) {
        ContactsEntity contactsEntity = new ContactsEntity();
        contactsEntity.setUserId(userId);
        contactsEntity.setGroupId(groupId);
        contactsEntity.setNote(note);
        contactsEntity.setContactsId(toUserId);

        this.baseMapper.insert(contactsEntity);
    }

    @Override
    public ContactsUserDto info(Long id) {
        return this.baseMapper.queryById(id);
    }

    @Override
    public void updateContacts(Long userId, Long id, Long groupId, String note, Integer status) {
        ContactsEntity contactsEntity = this.baseMapper.selectById(id);
        if (!userId.equals(contactsEntity.getUserId())){
            throw new MyException("身份验证出错");
        }

        if (contactsEntity.getGroupId() != null){
            contactsEntity.setGroupId(groupId);
        }
        if (contactsEntity.getNote() != null){
            contactsEntity.setNote(note);
        }
        if (contactsEntity.getStatus() != null){
            contactsEntity.setStatus(status);
        }

        this.baseMapper.updateById(contactsEntity);
    }

    @Override
    public PageUtil adminContactsList(Map<String, Object> params) {
        QueryWrapper<ContactsEntity> groupEntityQueryWrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if (key != null && !key.isEmpty()){
            UserEntity userEntity = userService.getOne(new QueryWrapper<UserEntity>().eq("username", key));
            if (userEntity == null){
                return new PageUtil(new ArrayList<>(), 0, 0, 0, 0);
            }
            groupEntityQueryWrapper.eq("user_id", userEntity.getId());
        }

        IPage<ContactsEntity> page = this.page(
                new Query<ContactsEntity>().getPage(params),
                groupEntityQueryWrapper
        );

        return new PageUtil(page);
    }

    @Override
    public void adminUpdateContacts(AdminUpdateContactsReq adminUpdateContactsReq) {
        ContactsEntity contactsEntity = this.baseMapper.selectById(adminUpdateContactsReq.getId());
        BeanUtils.copyProperties(adminUpdateContactsReq, contactsEntity);
        this.baseMapper.updateById(contactsEntity);
    }

    @Override
    public void adminDeleteContacts(List<Long> ids, Integer status) {
        this.baseMapper.deleteContactsByIds(ids, status);
    }
}